<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * ExtensionModel.class数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-24> SoChishun Added.
 */
class ExtensionModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tuser_member';

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'seat_export' => array(
            'field' => "siteID as '租户ID',userName as '工号',personalName as '姓名',extensionNumber as '分机号',teamNumber as '队列号',ctiModel as '班长模式',state as '状态',createdTime as '创建时间'",
            'order' => 'siteID',
            'cache' => true,
        ),
        'seat_item_list' => array(
            'field' => 'id,siteID,userName,personalName,extensionNumber,teamNumber,ctiModel,state,createdTime',
            'order' => 'userName',
        ),
        'extension_item_list' => array(
            'field' => 'id,userName,extensionPassword,ctiTrunk,ctiCallerNo,sipPort,recState,ctiRegMode,activated,forwardingState,forwardingExtension,createdTime,siteID',
            'order' => 'userName',
        ),
        'extension_export' => array(
            'field' => "siteID as '租户ID',userName as '分机号',extensionPassword as '分机密码',ctiTrunk as '中继名称',ctiCallerNo as '主叫号码',sipPort as '注册端口',recState as '录音标识',ctiRegMode as '中继注册模式',activated as '激活状态',forwardingState as '呼叫转移',createdTime as '创建时间'",
            'order' => 'siteID',
            'cache' => true,
        ),
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    public function scope_fields($scope = 'idtext') {
        return $this->base_get_scope_fields($scope);
    }

    /**
     * 返回列表
     * @return array
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_extension_list($where) {
        $orderby = I('sort', $this->_scope['extension_item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count('id');
        $list = $this->cache(true)->scope('extension_item_list')->where($where)->order($orderby)->page($page)->select();
        $out = array();
        foreach ($list as $row) {
            $row['recStateText'] = sofn_get_state_text('yes-no', $row['recState']);
            $row['ctiRegModeText'] = get_regMode_text($row['ctiRegMode']);
            $row['activatedText'] = get_activateState_text($row['activated']);
            $row['forwardingStateText'] = sofn_get_state_text('cti-call-forwarding-type', $row['forwardingState']) . ' ' . $row['forwardingExtension'];
            $row['siteIDText'] = get_siteID_text($row['siteID']);
            $out[] = $row;
        }
        return array('total' => $total, 'rows' => $out);
    }
    
    public function get_seat_list($where) {
        $orderby = I('sort', $this->_scope['seat_item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->cache(true)->where($where)->count('id');
        $list = $this->cache(true)->scope('seat_item_list')->where($where)->order($orderby)->page($page)->select();
        $out = array();
        foreach ($list as $row) {
            $row['ctiModelText'] = get_isLeader_text($row['ctiModel']);
            $row['siteIDText'] = get_siteID_text($row['siteID']);
            $out[] = $row;
        }
        return array('total' => $total, 'rows' => $out);
    }

}
